package com.qf.jie.service.impl;

import com.qf.jie.dao.UserDao;
import com.qf.jie.dao.impl.UserDaoImpl;
import com.qf.jie.entity.po.User;
import com.qf.jie.entity.vo.RespBean;
import com.qf.jie.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();
    @Override
    public RespBean login(HttpServletRequest req, String name, String password) {
        int id = userDao.getIdByNameAndPassword(name, password);
        if (id == 0){
            return RespBean.respError("用户名或密码错误");
        }
        List<User> user = userDao.getBeanById(id);
        if (user.size() == 0){
            return RespBean.respError("登陆失败，未知错误");
        }
        req.getSession().setAttribute("user", user.get(0));
        return RespBean.respSuccess("登录成功");
    }

    @Override
    public RespBean register(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        if (username == null || password == null || username.trim().equals("") || password.trim().equals("")){
            return RespBean.respError("用户名和密码不能为空");
        }
        int count = userDao.getCountByName(username);
        if (count > 0){
            return RespBean.respError("用户名已被占用");
        }
        int num = userDao.addBean(user);
        if (num == 0){
            return RespBean.respError("注册失败，未知错误");
        }
        return RespBean.respSuccess("注册成功");
    }

    @Override
    public RespBean getPage(Object currentPage, Object len) {

        return null;
    }


    //修改个人资料(username, user_type, sex, phone)
    @Override
    public RespBean updateUser(HttpServletRequest req, String username, String strUser_type, String sex, String phone) {
        // 处理前端不传参数或参数不是数字，传0则为0，传其它或者不传则为1
        int user_type = 1;
        if (strUser_type == null || strUser_type.equals("0")){
            user_type = 0;
        }
        if (username == null || username.trim().equals("") || sex == null || sex.trim().equals("") ||phone == null || phone.trim().equals("") ){
            return RespBean.respError("参数存在空值");
        }
        User user = (User) req.getSession().getAttribute("user");
        user.setUsername(username);
        user.setUser_type(user_type);
        user.setSex(sex);
        user.setPhone(phone);
        int num = userDao.updateById(user);
        if (num == 0){
            return RespBean.respError("修改失败");
        }
        return RespBean.respSuccess("修改成功");
    }

    @Override
    public RespBean updatePassword(HttpServletRequest req, String oldPassword, String newPassword) {
        User user = (User)req.getSession().getAttribute("user");
        String password = user.getPassword();
        if (!password.equals(oldPassword)){
            return RespBean.respError("旧密码错误");
        }
        if (password.equals(newPassword)){
            return RespBean.respError("新密码不能跟旧密码相同");
        }
        if (newPassword == null || newPassword.trim().equals("")){
            return RespBean.respError("新密码不能为空");
        }
        user.setPassword(newPassword);
        int num = userDao.updateById(user);
        if (num == 0){
            return RespBean.respError("修改失败，未知错误");
        }
        // 修改密码成功后将当前账号下线
        req.getSession().setAttribute("user", null);
        return RespBean.respSuccess("修改成功");
    }

    @Override
    public RespBean addMoney(HttpServletRequest req, String strMoney, String password) {
        try{
            double money = Double.parseDouble(strMoney);
            User userToSession = (User)req.getSession().getAttribute("user");
            List<User> users = userDao.getBeanById(userToSession.getId());
            if (users.size() == 0){
                return RespBean.respError("充值失败，未知错误");
            }
            if ( !password.equals(users.get(0).getPassword()) ){
                return RespBean.respError("充值失败，密码错误");
            }
            User user = users.get(0);
            user.setMoney(money + user.getMoney());
            int num = userDao.updateById(user);
            if (num == 0){
                return RespBean.respError("充值失败，未知错误");
            }
            // 实时更新session中的user对象的信息
            req.getSession().setAttribute("user", user);
            return RespBean.respSuccess("充值成功");
        }catch (Exception e){
            return RespBean.respError("参数类型错误:" + strMoney);
        }
    }

}
